﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace Project_Udrinkwater.PagesAdmin
{
    public partial class calculateSalary : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void bt_Calculate_Click(object sender, EventArgs e)
        {
            string ChDate = DateTime.Now.ToString("yyyy", new System.Globalization.CultureInfo("th-TH"));
            string ChMouth = DateTime.Now.ToString("MM", new System.Globalization.CultureInfo("th-TH"));
            if (Convert.ToInt32(CB_Year.Value.ToString()) <= Convert.ToInt32(ChDate))
            {
                if (Convert.ToInt32(CB_Month.Value.ToString()) >= Convert.ToInt32(ChMouth))
                {
                    
                if ((CB_Month.Value.ToString() == "01") || (CB_Month.Value.ToString() == "03") || (CB_Month.Value.ToString() == "05") || (CB_Month.Value.ToString() == "07") || (CB_Month.Value.ToString() == "08") || (CB_Month.Value.ToString() == "10") || (CB_Month.Value.ToString() == "12"))
                {
                    string ChDay = DateTime.Now.ToString("dd", new System.Globalization.CultureInfo("th-TH"));
                    if (Convert.ToInt32(ChDay) > 30)
                    {
                        //Response.Write(@"<script language='javascript'>alert('31')</script>");
                        ClassSalary CsSalary = new ClassSalary();
                        DataTable SalaryCH = new DataTable();
                        SalaryCH = CsSalary.GetData_ShowEmployee(CB_Month.Value.ToString(), (Convert.ToInt32(CB_Year.Value) - 543).ToString());
                        if (SalaryCH.Rows.Count == 0)
                        {
                            DataTable dt = CsSalary.CheckDayOfYear(CB_Year.SelectedItem.Text);
                            string Year = dt.Rows[0]["DayCount"].ToString();
                            DataTable Numemp = CsSalary.GetData_NumberEmployee();

                            for (int i = 0; i < Numemp.Rows.Count; i++)
                            {
                                Data Data = new Data();
                                InsertTime Insert = new InsertTime();
                                string Employee = Numemp.Rows[i]["Employee_ID"].ToString();
                                Data.Empoyee_ID = Employee;
                                string YearTime = (Convert.ToInt32(CB_Year.SelectedItem.Text) - 543).ToString();
                                DataTable Reduce_Absence = CsSalary.GetData_Reduce_Absence31(Employee, CB_Month.Value.ToString(), YearTime);
                                DataTable Reduce_Withdraw = CsSalary.GetData_Reduce_Withdraw31(Employee, CB_Month.Value.ToString(), YearTime);
                                //-------OT
                                DataTable Reduce_OT = CsSalary.GetData_Reduce_Overtime31(Employee, CB_Month.Value.ToString(), YearTime);
                                string CheckOT = Reduce_OT.Rows[0]["TOTAL"].ToString();
                                if (CheckOT !="")
                                {
                                    Data.Overtime = Convert.ToInt32(CheckOT);
                                }
                                else
                                {
                                    Data.Overtime = 0;
                                }
                                string Check = Reduce_Withdraw.Rows[0]["TotalEmp"].ToString();

                                //คำนวณค่า Commission
                                DataTable Com1 = CsSalary.GetData_Reduce_Commission31(Employee, CB_Month.Value.ToString(), YearTime, 1);//ถัง
                                DataTable Com2 = CsSalary.GetData_Reduce_Commission31(Employee, CB_Month.Value.ToString(), YearTime, 2);//ลัง
                                DataTable Com3 = CsSalary.GetData_Reduce_Commission31(Employee, CB_Month.Value.ToString(), YearTime, 3);//แพ็ค
                                DataTable Com4 = CsSalary.GetData_Reduce_Commission31(Employee, CB_Month.Value.ToString(), YearTime, 4);//โหล
                                string ComTank = Com1.Rows[0]["TOTAL"].ToString(); //ถัง
                                string ComCrate = Com2.Rows[0]["TOTAL"].ToString(); //ลัง
                                string Compack = Com3.Rows[0]["TOTAL"].ToString(); //แพ็ค
                                string Comdozen = Com4.Rows[0]["TOTAL"].ToString(); //โหล
                                //ตรวจสอบ Commission-----------------------------------------------------------------
                                if (ComTank != "")//ถัง
                                {
                                    Data.Commission += Convert.ToDouble(ComTank) * 1.50;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (ComCrate != "")//ลัง
                                {
                                    Data.Commission += Convert.ToDouble(ComCrate) * 2;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (Compack != "")//แพ็ค
                                {
                                    Data.Commission += Convert.ToDouble(Compack) * 1;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (Comdozen != "")//โหล
                                {
                                    Data.Commission += (Convert.ToDouble(Comdozen) / 2) * 1;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                //----------------------------------------------------------------------------------

                                
                                if (Check != "")
                                {
                                    Data.Reduce_Withdraw = Convert.ToInt32(Reduce_Withdraw.Rows[0]["TotalEmp"].ToString());  //เบิกล่วงหน้า
                                }
                                else
                                {
                                    Data.Reduce_Withdraw = 0;
                                }


                                Data.Other = 100;//ค่าประกันสังคม
                                Data.Start_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                Data.End_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                //------------------------ขาดงานเกินเวลา-----------------------------
                                switch (CB_Month.Value.ToString())
                                {
                                    case "01": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "02": if (Year == "365")
                                        {
                                            if (Reduce_Absence.Rows.Count >= 26)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (26 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                        }
                                        if (Year == "366")
                                        {
                                            if (Reduce_Absence.Rows.Count >= 27)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (27 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                        }
                                        break;
                                    case "03": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "04": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "05": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "06": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "07": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "08": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "09": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "10": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "11": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "12": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;

                                }
                                Insert.Insert_DataSalaryDetial(Data);

                            }
                            DataTable Showdt = new DataTable();
                            Showdt = CsSalary.GetData_ShowEmployee(CB_Month.Value.ToString(), (Convert.ToInt32(CB_Year.Value) - 543).ToString());
                            GV_calculateSalary.DataSource = Showdt;
                            GV_calculateSalary.DataBind();
                        }
                        else
                        {
                            GV_calculateSalary.DataSource = SalaryCH;
                            GV_calculateSalary.DataBind();
                        }
                    }
                    else
                    {
                        Response.Write(@"<script language='javascript'>alert('ยังไม่สามารถคำนวณเงินเดือนได้')</script>");
                    }
                }
                else if (CB_Month.Value.ToString() =="02")
                {
                    ClassSalary CsSalary = new ClassSalary();
                    DataTable dt = CsSalary.CheckDayOfYear(CB_Year.SelectedItem.Text);
                    string Year = dt.Rows[0]["DayCount"].ToString();

                    if (Year == "365")
                    {
                        if (Convert.ToInt32(ChDate)==28)
                        {
                             //ClassSalary CsSalary = new ClassSalary();
                        DataTable SalaryCH = new DataTable();
                        SalaryCH = CsSalary.GetData_ShowEmployee(CB_Month.Value.ToString(), (Convert.ToInt32(CB_Year.Value) - 543).ToString());
                        if (SalaryCH.Rows.Count == 0)
                        {
                            //DataTable dt = CsSalary.CheckDayOfYear(CB_Year.SelectedItem.Text);
                            //string Year = dt.Rows[0]["DayCount"].ToString();
                            DataTable Numemp = CsSalary.GetData_NumberEmployee();

                            for (int i = 0; i < Numemp.Rows.Count; i++)
                            {
                                Data Data = new Data();
                                InsertTime Insert = new InsertTime();
                                string Employee = Numemp.Rows[i]["Employee_ID"].ToString();
                                Data.Empoyee_ID = Employee;
                                string YearTime = (Convert.ToInt32(CB_Year.SelectedItem.Text) - 543).ToString();
                                DataTable Reduce_Absence = CsSalary.GetData_Reduce_Absence28(Employee, CB_Month.Value.ToString(), YearTime);
                                DataTable Reduce_Withdraw = CsSalary.GetData_Reduce_Withdraw28(Employee, CB_Month.Value.ToString(), YearTime);
                                //-------OT
                                DataTable Reduce_OT = CsSalary.GetData_Reduce_Overtime28(Employee, CB_Month.Value.ToString(), YearTime);
                                string CheckOT = Reduce_OT.Rows[0]["TOTAL"].ToString();
                                if (CheckOT != "")
                                {
                                    Data.Overtime = Convert.ToInt32(CheckOT);
                                }
                                else
                                {
                                    Data.Overtime = 0;
                                }
                                string Check = Reduce_Withdraw.Rows[0]["TotalEmp"].ToString();

                                //คำนวณค่า Commission
                                DataTable Com1 = CsSalary.GetData_Reduce_Commission28(Employee, CB_Month.Value.ToString(), YearTime, 1);//ถัง
                                DataTable Com2 = CsSalary.GetData_Reduce_Commission28(Employee, CB_Month.Value.ToString(), YearTime, 2);//ลัง
                                DataTable Com3 = CsSalary.GetData_Reduce_Commission28(Employee, CB_Month.Value.ToString(), YearTime, 3);//แพ็ค
                                DataTable Com4 = CsSalary.GetData_Reduce_Commission28(Employee, CB_Month.Value.ToString(), YearTime, 4);//โหล
                                string ComTank = Com1.Rows[0]["TOTAL"].ToString(); //ถัง
                                string ComCrate = Com2.Rows[0]["TOTAL"].ToString(); //ลัง
                                string Compack = Com3.Rows[0]["TOTAL"].ToString(); //แพ็ค
                                string Comdozen = Com4.Rows[0]["TOTAL"].ToString(); //โหล
                                //ตรวจสอบ Commission-----------------------------------------------------------------
                                if (ComTank != "")//ถัง
                                {
                                    Data.Commission += Convert.ToDouble(ComTank) * 1.50;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (ComCrate != "")//ลัง
                                {
                                    Data.Commission += Convert.ToDouble(ComCrate) * 2;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (Compack != "")//แพ็ค
                                {
                                    Data.Commission += Convert.ToDouble(Compack) * 1;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (Comdozen != "")//โหล
                                {
                                    Data.Commission += (Convert.ToDouble(Comdozen) / 2) * 1;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                //----------------------------------------------------------------------------------
                                if (Check != "")
                                {
                                    Data.Reduce_Withdraw = Convert.ToInt32(Reduce_Withdraw.Rows[0]["TotalEmp"].ToString());  //เบิกล่วงหน้า
                                }
                                else
                                {
                                    Data.Reduce_Withdraw = 0;
                                }


                                Data.Other = 100;//ค่าประกันสังคม
                                Data.Start_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                Data.End_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                //------------------------ขาดงานเกินเวลา-----------------------------
                                switch (CB_Month.Value.ToString())
                                {
                                    case "01": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "02": if (Year == "365")
                                        {
                                            if (Reduce_Absence.Rows.Count >= 26)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (26 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                        }
                                        if (Year == "366")
                                        {
                                            if (Reduce_Absence.Rows.Count >= 27)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (27 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                        }
                                        break;
                                    case "03": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "04": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "05": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "06": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "07": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "08": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "09": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "10": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "11": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "12": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;

                                }
                                Insert.Insert_DataSalaryDetial(Data);

                            }
                            DataTable Showdt = new DataTable();
                            Showdt = CsSalary.GetData_ShowEmployee(CB_Month.Value.ToString(), (Convert.ToInt32(CB_Year.Value) - 543).ToString());
                            GV_calculateSalary.DataSource = Showdt;
                            GV_calculateSalary.DataBind();
                        }
                        else
                        {
                            GV_calculateSalary.DataSource = SalaryCH;
                            GV_calculateSalary.DataBind();
                        }
                    }
                        }
                        else
                        {
                            Response.Write(@"<script language='javascript'>alert('ยังไม่สามารถคำนวณเงินเดือนได้')</script>");
                        }
                        
                    
                    if (Year == "366")
                    {
                        DataTable SalaryCH = new DataTable();
                             SalaryCH = CsSalary.GetData_ShowEmployee(CB_Month.Value.ToString(), (Convert.ToInt32(CB_Year.Value) - 543).ToString());
                        if (Convert.ToInt32(ChDate) == 29)
                        {
                             
                              DataTable Numemp = CsSalary.GetData_NumberEmployee();

                            for (int i = 0; i < Numemp.Rows.Count; i++)
                            {
                                Data Data = new Data();
                                InsertTime Insert = new InsertTime();
                                string Employee = Numemp.Rows[i]["Employee_ID"].ToString();
                                Data.Empoyee_ID = Employee;
                                string YearTime = (Convert.ToInt32(CB_Year.SelectedItem.Text) - 543).ToString();
                                DataTable Reduce_Absence = CsSalary.GetData_Reduce_Absence29(Employee, CB_Month.Value.ToString(), YearTime);
                                DataTable Reduce_Withdraw = CsSalary.GetData_Reduce_Withdraw29(Employee, CB_Month.Value.ToString(), YearTime);
                                //-------OT
                                DataTable Reduce_OT = CsSalary.GetData_Reduce_Overtime29(Employee, CB_Month.Value.ToString(), YearTime);
                                string CheckOT = Reduce_OT.Rows[0]["TOTAL"].ToString();
                                if (CheckOT != "")
                                {
                                    Data.Overtime = Convert.ToInt32(CheckOT);
                                }
                                else
                                {
                                    Data.Overtime = 0;
                                }
                                string Check = Reduce_Withdraw.Rows[0]["TotalEmp"].ToString();
                                //คำนวณค่า Commission
                                DataTable Com1 = CsSalary.GetData_Reduce_Commission29(Employee, CB_Month.Value.ToString(), YearTime, 1);//ถัง
                                DataTable Com2 = CsSalary.GetData_Reduce_Commission29(Employee, CB_Month.Value.ToString(), YearTime, 2);//ลัง
                                DataTable Com3 = CsSalary.GetData_Reduce_Commission29(Employee, CB_Month.Value.ToString(), YearTime, 3);//แพ็ค
                                DataTable Com4 = CsSalary.GetData_Reduce_Commission29(Employee, CB_Month.Value.ToString(), YearTime, 4);//โหล
                                string ComTank = Com1.Rows[0]["TOTAL"].ToString(); //ถัง
                                string ComCrate = Com2.Rows[0]["TOTAL"].ToString(); //ลัง
                                string Compack = Com3.Rows[0]["TOTAL"].ToString(); //แพ็ค
                                string Comdozen = Com4.Rows[0]["TOTAL"].ToString(); //โหล
                                //ตรวจสอบ Commission-----------------------------------------------------------------
                                if (ComTank != "")//ถัง
                                {
                                    Data.Commission += Convert.ToDouble(ComTank) * 1.50;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (ComCrate != "")//ลัง
                                {
                                    Data.Commission += Convert.ToDouble(ComCrate) * 2;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (Compack != "")//แพ็ค
                                {
                                    Data.Commission += Convert.ToDouble(Compack) * 1;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (Comdozen != "")//โหล
                                {
                                    Data.Commission += (Convert.ToDouble(Comdozen) / 2) * 1;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                //----------------------------------------------------------------------------------
                                if (Check != "")
                                {
                                    Data.Reduce_Withdraw = Convert.ToInt32(Reduce_Withdraw.Rows[0]["TotalEmp"].ToString());  //เบิกล่วงหน้า
                                }
                                else
                                {
                                    Data.Reduce_Withdraw = 0;
                                }


                                Data.Other = 100;//ค่าประกันสังคม
                                Data.Start_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                Data.End_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                //------------------------ขาดงานเกินเวลา-----------------------------
                                switch (CB_Month.Value.ToString())
                                {
                                    case "01": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "02": if (Year == "365")
                                        {
                                            if (Reduce_Absence.Rows.Count >= 26)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (26 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                        }
                                        if (Year == "366")
                                        {
                                            if (Reduce_Absence.Rows.Count >= 27)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (27 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                        }
                                        break;
                                    case "03": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "04": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "05": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "06": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "07": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "08": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "09": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "10": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "11": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "12": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;

                                }
                                Insert.Insert_DataSalaryDetial(Data);

                            }
                            DataTable Showdt = new DataTable();
                            Showdt = CsSalary.GetData_ShowEmployee(CB_Month.Value.ToString(), (Convert.ToInt32(CB_Year.Value) - 543).ToString());
                            GV_calculateSalary.DataSource = Showdt;
                            GV_calculateSalary.DataBind();
                        }
                        else
                        {
                            GV_calculateSalary.DataSource = SalaryCH;
                            GV_calculateSalary.DataBind();
                        }
                        }
                        else
                        {
                            Response.Write(@"<script language='javascript'>alert('ยังไม่สามารถคำนวณเงินเดือนได้')</script>");
                        }
                        
                    }
                
              
                }
                else
                {
                    ClassSalary CsSalary = new ClassSalary();
                    DataTable SalaryCH = new DataTable();
                    SalaryCH = CsSalary.GetData_ShowEmployee(CB_Month.Value.ToString(), (Convert.ToInt32(CB_Year.Value) - 543).ToString());
                    if (SalaryCH.Rows.Count == 0)
                    {
                        DataTable dt = CsSalary.CheckDayOfYear(CB_Year.SelectedItem.Text);
                        string Year = dt.Rows[0]["DayCount"].ToString();
                        DataTable Numemp = CsSalary.GetData_NumberEmployee();

                        for (int i = 0; i < Numemp.Rows.Count; i++)
                        {
                            Data Data = new Data();
                            InsertTime Insert = new InsertTime();
                            string Employee = Numemp.Rows[i]["Employee_ID"].ToString();
                            Data.Empoyee_ID = Employee;
                            string YearTime = (Convert.ToInt32(CB_Year.SelectedItem.Text) - 543).ToString();

                            if ((CB_Month.Value.ToString() == "01") || (CB_Month.Value.ToString() == "03") || (CB_Month.Value.ToString() == "05") || (CB_Month.Value.ToString() == "07") || (CB_Month.Value.ToString() == "08") || (CB_Month.Value.ToString() == "10") || (CB_Month.Value.ToString() == "12"))
                            {
                                DataTable Reduce_Absence = CsSalary.GetData_Reduce_Absence31(Employee, CB_Month.Value.ToString(), YearTime);
                                DataTable Reduce_Withdraw = CsSalary.GetData_Reduce_Withdraw31(Employee, CB_Month.Value.ToString(), YearTime);
                                DataTable Reduce_OT = CsSalary.GetData_Reduce_Overtime31(Employee, CB_Month.Value.ToString(), YearTime);
                                DataTable Com1 = CsSalary.GetData_Reduce_Commission31(Employee, CB_Month.Value.ToString(), YearTime, 1);//ถัง
                                DataTable Com2 = CsSalary.GetData_Reduce_Commission31(Employee, CB_Month.Value.ToString(), YearTime, 2);//ลัง
                                DataTable Com3 = CsSalary.GetData_Reduce_Commission31(Employee, CB_Month.Value.ToString(), YearTime, 3);//แพ็ค
                                DataTable Com4 = CsSalary.GetData_Reduce_Commission31(Employee, CB_Month.Value.ToString(), YearTime, 4);//โหล

                                string CheckOT = Reduce_OT.Rows[0]["TOTAL"].ToString();

                                if (CheckOT != "")
                                {
                                    Data.Overtime = Convert.ToInt32(CheckOT);
                                }
                                else
                                {
                                    Data.Overtime = 0;
                                }
                                string Check = Reduce_Withdraw.Rows[0]["TotalEmp"].ToString();
                                //คำนวณค่า Commission

                                string ComTank = Com1.Rows[0]["TOTAL"].ToString(); //ถัง
                                string ComCrate = Com2.Rows[0]["TOTAL"].ToString(); //ลัง
                                string Compack = Com3.Rows[0]["TOTAL"].ToString(); //แพ็ค
                                string Comdozen = Com4.Rows[0]["TOTAL"].ToString(); //โหล
                                //ตรวจสอบ Commission-----------------------------------------------------------------
                                if (ComTank != "")//ถัง
                                {
                                    Data.Commission += Convert.ToDouble(ComTank) * 1.50;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (ComCrate != "")//ลัง
                                {
                                    Data.Commission += Convert.ToDouble(ComCrate) * 2;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (Compack != "")//แพ็ค
                                {
                                    Data.Commission += Convert.ToDouble(Compack) * 1;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (Comdozen != "")//โหล
                                {
                                    Data.Commission += (Convert.ToDouble(Comdozen) / 2) * 1;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                //----------------------------------------------------------------------------------
                                if (Check != "")
                                {
                                    Data.Reduce_Withdraw = Convert.ToInt32(Reduce_Withdraw.Rows[0]["TotalEmp"].ToString());  //เบิกล่วงหน้า
                                }
                                else
                                {
                                    Data.Reduce_Withdraw = 0;
                                }


                                Data.Other = 100;//ค่าประกันสังคม
                                Data.Start_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                Data.End_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                //------------------------ขาดงานเกินเวลา-----------------------------
                                switch (CB_Month.Value.ToString())
                                {
                                    case "01": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "02": if (Year == "365")
                                        {
                                            if (Reduce_Absence.Rows.Count >= 26)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (26 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                        }
                                        if (Year == "366")
                                        {
                                            if (Reduce_Absence.Rows.Count >= 27)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (27 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                        }
                                        break;
                                    case "03": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "04": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "05": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "06": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "07": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "08": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "09": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "10": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "11": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "12": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;

                                }
                                Insert.Insert_DataSalaryDetial(Data);
                            }
                            else if (CB_Month.Value.ToString() =="02")
                            {
                                if (Year == "365")
                                {
                                    DataTable Reduce_Absence = CsSalary.GetData_Reduce_Absence28(Employee, CB_Month.Value.ToString(), YearTime);
                                    DataTable Reduce_Withdraw = CsSalary.GetData_Reduce_Withdraw28(Employee, CB_Month.Value.ToString(), YearTime);
                                    DataTable Reduce_OT = CsSalary.GetData_Reduce_Overtime28(Employee, CB_Month.Value.ToString(), YearTime);
                                    DataTable Com1 = CsSalary.GetData_Reduce_Commission28(Employee, CB_Month.Value.ToString(), YearTime, 1);//ถัง
                                    DataTable Com2 = CsSalary.GetData_Reduce_Commission28(Employee, CB_Month.Value.ToString(), YearTime, 2);//ลัง
                                    DataTable Com3 = CsSalary.GetData_Reduce_Commission28(Employee, CB_Month.Value.ToString(), YearTime, 3);//แพ็ค
                                    DataTable Com4 = CsSalary.GetData_Reduce_Commission28(Employee, CB_Month.Value.ToString(), YearTime, 4);//โหล

                                    string CheckOT = Reduce_OT.Rows[0]["TOTAL"].ToString();

                                    if (CheckOT != "")
                                    {
                                        Data.Overtime = Convert.ToInt32(CheckOT);
                                    }
                                    else
                                    {
                                        Data.Overtime = 0;
                                    }
                                    string Check = Reduce_Withdraw.Rows[0]["TotalEmp"].ToString();
                                    //คำนวณค่า Commission

                                    string ComTank = Com1.Rows[0]["TOTAL"].ToString(); //ถัง
                                    string ComCrate = Com2.Rows[0]["TOTAL"].ToString(); //ลัง
                                    string Compack = Com3.Rows[0]["TOTAL"].ToString(); //แพ็ค
                                    string Comdozen = Com4.Rows[0]["TOTAL"].ToString(); //โหล
                                    //ตรวจสอบ Commission-----------------------------------------------------------------
                                    if (ComTank != "")//ถัง
                                    {
                                        Data.Commission += Convert.ToDouble(ComTank) * 1.50;
                                    }
                                    else
                                    {
                                        Data.Commission += 0;
                                    }
                                    if (ComCrate != "")//ลัง
                                    {
                                        Data.Commission += Convert.ToDouble(ComCrate) * 2;
                                    }
                                    else
                                    {
                                        Data.Commission += 0;
                                    }
                                    if (Compack != "")//แพ็ค
                                    {
                                        Data.Commission += Convert.ToDouble(Compack) * 1;
                                    }
                                    else
                                    {
                                        Data.Commission += 0;
                                    }
                                    if (Comdozen != "")//โหล
                                    {
                                        Data.Commission += (Convert.ToDouble(Comdozen) / 2) * 1;
                                    }
                                    else
                                    {
                                        Data.Commission += 0;
                                    }
                                    //----------------------------------------------------------------------------------
                                    if (Check != "")
                                    {
                                        Data.Reduce_Withdraw = Convert.ToInt32(Reduce_Withdraw.Rows[0]["TotalEmp"].ToString());  //เบิกล่วงหน้า
                                    }
                                    else
                                    {
                                        Data.Reduce_Withdraw = 0;
                                    }


                                    Data.Other = 100;//ค่าประกันสังคม
                                    Data.Start_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                    Data.End_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                    //------------------------ขาดงานเกินเวลา-----------------------------
                                    switch (CB_Month.Value.ToString())
                                    {
                                        case "01": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "02": if (Year == "365")
                                            {
                                                if (Reduce_Absence.Rows.Count >= 26)
                                                {
                                                    Data.Reduce_Absence = 0;

                                                }
                                                else
                                                {
                                                    Data.Reduce_Absence = (26 - Reduce_Absence.Rows.Count) * 200;

                                                }
                                            }
                                            if (Year == "366")
                                            {
                                                if (Reduce_Absence.Rows.Count >= 27)
                                                {
                                                    Data.Reduce_Absence = 0;

                                                }
                                                else
                                                {
                                                    Data.Reduce_Absence = (27 - Reduce_Absence.Rows.Count) * 200;

                                                }
                                            }
                                            break;
                                        case "03": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "04": if (Reduce_Absence.Rows.Count >= 28)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "05": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "06": if (Reduce_Absence.Rows.Count >= 28)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "07": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "08": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "09": if (Reduce_Absence.Rows.Count >= 28)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "10": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "11": if (Reduce_Absence.Rows.Count >= 28)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "12": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;

                                    }
                                    Insert.Insert_DataSalaryDetial(Data);
                                }
                                if (Year =="366")
                                {
                                    DataTable Reduce_Absence = CsSalary.GetData_Reduce_Absence29(Employee, CB_Month.Value.ToString(), YearTime);
                                    DataTable Reduce_Withdraw = CsSalary.GetData_Reduce_Withdraw29(Employee, CB_Month.Value.ToString(), YearTime);
                                    DataTable Reduce_OT = CsSalary.GetData_Reduce_Overtime29(Employee, CB_Month.Value.ToString(), YearTime);
                                    DataTable Com1 = CsSalary.GetData_Reduce_Commission29(Employee, CB_Month.Value.ToString(), YearTime, 1);//ถัง
                                    DataTable Com2 = CsSalary.GetData_Reduce_Commission29(Employee, CB_Month.Value.ToString(), YearTime, 2);//ลัง
                                    DataTable Com3 = CsSalary.GetData_Reduce_Commission29(Employee, CB_Month.Value.ToString(), YearTime, 3);//แพ็ค
                                    DataTable Com4 = CsSalary.GetData_Reduce_Commission29(Employee, CB_Month.Value.ToString(), YearTime, 4);//โหล

                                    string CheckOT = Reduce_OT.Rows[0]["TOTAL"].ToString();

                                    if (CheckOT != "")
                                    {
                                        Data.Overtime = Convert.ToInt32(CheckOT);
                                    }
                                    else
                                    {
                                        Data.Overtime = 0;
                                    }
                                    string Check = Reduce_Withdraw.Rows[0]["TotalEmp"].ToString();
                                    //คำนวณค่า Commission

                                    string ComTank = Com1.Rows[0]["TOTAL"].ToString(); //ถัง
                                    string ComCrate = Com2.Rows[0]["TOTAL"].ToString(); //ลัง
                                    string Compack = Com3.Rows[0]["TOTAL"].ToString(); //แพ็ค
                                    string Comdozen = Com4.Rows[0]["TOTAL"].ToString(); //โหล
                                    //ตรวจสอบ Commission-----------------------------------------------------------------
                                    if (ComTank != "")//ถัง
                                    {
                                        Data.Commission += Convert.ToDouble(ComTank) * 1.50;
                                    }
                                    else
                                    {
                                        Data.Commission += 0;
                                    }
                                    if (ComCrate != "")//ลัง
                                    {
                                        Data.Commission += Convert.ToDouble(ComCrate) * 2;
                                    }
                                    else
                                    {
                                        Data.Commission += 0;
                                    }
                                    if (Compack != "")//แพ็ค
                                    {
                                        Data.Commission += Convert.ToDouble(Compack) * 1;
                                    }
                                    else
                                    {
                                        Data.Commission += 0;
                                    }
                                    if (Comdozen != "")//โหล
                                    {
                                        Data.Commission += (Convert.ToDouble(Comdozen) / 2) * 1;
                                    }
                                    else
                                    {
                                        Data.Commission += 0;
                                    }
                                    //----------------------------------------------------------------------------------
                                    if (Check != "")
                                    {
                                        Data.Reduce_Withdraw = Convert.ToInt32(Reduce_Withdraw.Rows[0]["TotalEmp"].ToString());  //เบิกล่วงหน้า
                                    }
                                    else
                                    {
                                        Data.Reduce_Withdraw = 0;
                                    }


                                    Data.Other = 100;//ค่าประกันสังคม
                                    Data.Start_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                    Data.End_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                    //------------------------ขาดงานเกินเวลา-----------------------------
                                    switch (CB_Month.Value.ToString())
                                    {
                                        case "01": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "02": if (Year == "365")
                                            {
                                                if (Reduce_Absence.Rows.Count >= 26)
                                                {
                                                    Data.Reduce_Absence = 0;

                                                }
                                                else
                                                {
                                                    Data.Reduce_Absence = (26 - Reduce_Absence.Rows.Count) * 200;

                                                }
                                            }
                                            if (Year == "366")
                                            {
                                                if (Reduce_Absence.Rows.Count >= 27)
                                                {
                                                    Data.Reduce_Absence = 0;

                                                }
                                                else
                                                {
                                                    Data.Reduce_Absence = (27 - Reduce_Absence.Rows.Count) * 200;

                                                }
                                            }
                                            break;
                                        case "03": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "04": if (Reduce_Absence.Rows.Count >= 28)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "05": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "06": if (Reduce_Absence.Rows.Count >= 28)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "07": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "08": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "09": if (Reduce_Absence.Rows.Count >= 28)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "10": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "11": if (Reduce_Absence.Rows.Count >= 28)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;
                                        case "12": if (Reduce_Absence.Rows.Count >= 29)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                            break;

                                    }
                                    Insert.Insert_DataSalaryDetial(Data);
                                }
                            }
                            else
                            {
                                DataTable Reduce_Absence = CsSalary.GetData_Reduce_Absence30(Employee, CB_Month.Value.ToString(), YearTime);
                                DataTable Reduce_Withdraw = CsSalary.GetData_Reduce_Withdraw30(Employee, CB_Month.Value.ToString(), YearTime);
                                DataTable Reduce_OT = CsSalary.GetData_Reduce_Overtime30(Employee, CB_Month.Value.ToString(), YearTime);
                                DataTable Com1 = CsSalary.GetData_Reduce_Commission30(Employee, CB_Month.Value.ToString(), YearTime, 1);//ถัง
                                DataTable Com2 = CsSalary.GetData_Reduce_Commission30(Employee, CB_Month.Value.ToString(), YearTime, 2);//ลัง
                                DataTable Com3 = CsSalary.GetData_Reduce_Commission30(Employee, CB_Month.Value.ToString(), YearTime, 3);//แพ็ค
                                DataTable Com4 = CsSalary.GetData_Reduce_Commission30(Employee, CB_Month.Value.ToString(), YearTime, 4);//โหล

                                string CheckOT = Reduce_OT.Rows[0]["TOTAL"].ToString();

                                if (CheckOT != "")
                                {
                                    Data.Overtime = Convert.ToInt32(CheckOT);
                                }
                                else
                                {
                                    Data.Overtime = 0;
                                }
                                string Check = Reduce_Withdraw.Rows[0]["TotalEmp"].ToString();
                                //คำนวณค่า Commission

                                string ComTank = Com1.Rows[0]["TOTAL"].ToString(); //ถัง
                                string ComCrate = Com2.Rows[0]["TOTAL"].ToString(); //ลัง
                                string Compack = Com3.Rows[0]["TOTAL"].ToString(); //แพ็ค
                                string Comdozen = Com4.Rows[0]["TOTAL"].ToString(); //โหล
                                //ตรวจสอบ Commission-----------------------------------------------------------------
                                if (ComTank != "")//ถัง
                                {
                                    Data.Commission += Convert.ToDouble(ComTank) * 1.50;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (ComCrate != "")//ลัง
                                {
                                    Data.Commission += Convert.ToDouble(ComCrate) * 2;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (Compack != "")//แพ็ค
                                {
                                    Data.Commission += Convert.ToDouble(Compack) * 1;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                if (Comdozen != "")//โหล
                                {
                                    Data.Commission += (Convert.ToDouble(Comdozen) / 2) * 1;
                                }
                                else
                                {
                                    Data.Commission += 0;
                                }
                                //----------------------------------------------------------------------------------
                                if (Check != "")
                                {
                                    Data.Reduce_Withdraw = Convert.ToInt32(Reduce_Withdraw.Rows[0]["TotalEmp"].ToString());  //เบิกล่วงหน้า
                                }
                                else
                                {
                                    Data.Reduce_Withdraw = 0;
                                }


                                Data.Other = 100;//ค่าประกันสังคม
                                Data.Start_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                Data.End_Salary = Convert.ToDateTime(CB_Year.Value.ToString() + "/" + CB_Month.Value.ToString() + "/01");
                                //------------------------ขาดงานเกินเวลา-----------------------------
                                switch (CB_Month.Value.ToString())
                                {
                                    case "01": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "02": if (Year == "365")
                                        {
                                            if (Reduce_Absence.Rows.Count >= 26)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (26 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                        }
                                        if (Year == "366")
                                        {
                                            if (Reduce_Absence.Rows.Count >= 27)
                                            {
                                                Data.Reduce_Absence = 0;

                                            }
                                            else
                                            {
                                                Data.Reduce_Absence = (27 - Reduce_Absence.Rows.Count) * 200;

                                            }
                                        }
                                        break;
                                    case "03": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "04": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "05": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "06": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "07": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "08": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "09": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "10": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "11": if (Reduce_Absence.Rows.Count >= 28)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (28 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;
                                    case "12": if (Reduce_Absence.Rows.Count >= 29)
                                        {
                                            Data.Reduce_Absence = 0;

                                        }
                                        else
                                        {
                                            Data.Reduce_Absence = (29 - Reduce_Absence.Rows.Count) * 200;

                                        }
                                        break;

                                }
                                Insert.Insert_DataSalaryDetial(Data);
                            }
                           
                            

                        }
                        DataTable Showdt = new DataTable();
                        Showdt = CsSalary.GetData_ShowEmployee(CB_Month.Value.ToString(), (Convert.ToInt32(CB_Year.Value) - 543).ToString());
                        GV_calculateSalary.DataSource = Showdt;
                        GV_calculateSalary.DataBind();
                    }
                    else
                    {
                        GV_calculateSalary.DataSource = SalaryCH;
                        GV_calculateSalary.DataBind();
                    }
                }
            }
            else
            {
                Response.Write(@"<script language='javascript'>alert('ไม่มีข้อมูลที่ค้นหา')</script>");
               // GV_calculateSalary.DataBind();
            }

        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Report Admin/ShoeSalaryReport.aspx");
        }
      



    }
}